home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / plotting / gnuplot3.lzh / gnuplot / docs / doc2tex.c < prev    next >
C/C++ Source or Header  |  1991-08-23  |  5KB  |  247 lines

  1. /*
  2.  * doc2tex.c  -- program to convert Gnuplot .DOC format to LaTeX document
  3.  * Also will work for VMS .HLP files. 
  4.  * Modified by Russell Lang from hlp2ms.c by Thomas Williams 
  5.  * Extended by David Kotz to support quotes ("), backquotes, tables.
  6.  *
  7.  * usage:  doc2tex < file.doc > file.tex
  8.  *
  9.  *   where file.doc is a Gnuplot .DOC file, and file.tex will be an
  10.  *     article document suitable for printing with LaTeX.
  11.  *
  12.  * typical usage for GNUPLOT:
  13.  *
  14.  *   doc2tex < gnuplot.doc > gnuplot.tex 
  15.  *   latex gnuplot.tex ; latex gnuplot.tex
  16.  */
  17.  
  18. static char rcsid[] = "$Id: doc2tex.c,v 1.1 90/01/11 15:44:06 dfk Exp Locker: dfk $";
  19.  
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #ifdef AMIGA_LC_5_1
  23. #include <string.h>
  24. #endif
  25.  
  26. #define MAX_NAME_LEN    256
  27. #define MAX_LINE_LEN    256
  28. #define TRUE 1
  29. #define FALSE 0
  30.  
  31. typedef int boolean;
  32.  
  33. boolean intable = FALSE;
  34. boolean verb = FALSE;
  35.  
  36. main()
  37. {
  38.     init(stdout);
  39.     convert(stdin,stdout);
  40.     finish(stdout);
  41.     exit(0);
  42. }
  43.  
  44.  
  45. init(b)
  46. FILE *b;
  47. {
  48.     (void) fputs("\\input{titlepage.tex}\n",b);
  49. }
  50.  
  51.  
  52. convert(a,b)
  53.     FILE *a,*b;
  54. {
  55.     static char line[MAX_LINE_LEN];
  56.  
  57.     while (fgets(line,MAX_LINE_LEN,a)) {
  58.        process_line(line, b);
  59.     }
  60. }
  61.  
  62. process_line(line, b)
  63.     char *line;
  64.     FILE *b;
  65. {
  66.     switch(line[0]) {        /* control character */
  67.        case '?': {            /* interactive help entry */
  68.           break;            /* ignore */
  69.        }
  70.        case '@': {            /* start/end table */
  71.           if (intable) {
  72.              (void) fputs("\\hline\n\\end{tabular}\n", b);
  73.              (void) fputs("\\end{center}\n",b);
  74.              intable = FALSE;
  75.           } else {
  76.              if (verb) {
  77.                 (void) fputs("\\end{verbatim}\n",b);
  78.                 verb=FALSE;
  79.              } 
  80.              (void) fputs("\n\\begin{center}\n", b);
  81.              (void) fputs("\\begin{tabular}{|ccl|} \\hline\n", b);
  82.              intable = TRUE;
  83.           }
  84.           /* ignore rest of line */
  85.           break;
  86.        }
  87.        case '#': {            /* latex table entry */
  88.           if (intable)
  89.             (void) fputs(line+1, b); /* copy directly */
  90.           else
  91.             fprintf(stderr, "error: # line found outside of table\n");
  92.           break;
  93.        }
  94.        case '%': {            /* troff table entry */
  95.           break;            /* ignore */
  96.        }
  97.        case '\n':            /* empty text line */
  98.        case ' ': {            /* normal text line */
  99.           if (intable)
  100.             break;        /* ignore while in table */
  101.           if (line[1] == ' ') {
  102.              /* verbatim mode */
  103.              if (!verb) {
  104.                 (void) fputs("\\begin{verbatim}\n",b);
  105.                 verb=TRUE;
  106.              }
  107.              (void) fputs(line+1,b); 
  108.           } else {
  109.              if (verb) {
  110.                 (void) fputs("\\end{verbatim}\n",b);
  111.                 verb=FALSE;
  112.              } 
  113.              if (line[0] == '\n')
  114.                puttex(line,b); /* handle totally blank line */
  115.              else
  116.                puttex(line+1,b);
  117.           }
  118.           break;
  119.        }
  120.        default: {
  121.           if (isdigit(line[0])) { /* start of section */
  122.              if (!intable)    /* ignore while in table */
  123.                section(line, b);
  124.           } else
  125.             fprintf(stderr, "unknown control code '%c' in column 1\n", 
  126.                   line[0]);
  127.           break;
  128.        }
  129.     }
  130. }
  131.  
  132. /* process a line with a digit control char */
  133. /* starts a new [sub]section */
  134.  
  135. section(line, b)
  136.     char *line;
  137.     FILE *b;
  138. {
  139.     static char string[MAX_LINE_LEN];
  140.     int sh_i;
  141.  
  142.     if (verb) {
  143.        (void) fputs("\\end{verbatim}\n",b);
  144.        verb=FALSE;
  145.     } 
  146. #ifdef AMIGA_LC_5_1
  147.     (void) sscanf(line,"%d",&sh_i);
  148.     strcpy(string,strchr(line,' ')+1);
  149.     {
  150.       char *p;
  151.       p = strchr(string,'\n');
  152.       if (p != NULL) *p = '\0';
  153.     }
  154. #else
  155.     (void) sscanf(line,"%d %[^\n]s",&sh_i,string);
  156. #endif
  157.     switch(sh_i)
  158.      {
  159.         case 1: 
  160.         (void) fprintf(b,"\\section{");
  161.         break;
  162.         case 2: 
  163.         (void) fprintf(b,"\\section{");
  164.         break;
  165.         case 3:
  166.         (void) fprintf(b,"\\subsection{");
  167.         break;
  168.         case 4: 
  169.         (void) fprintf(b,"\\subsubsection{");
  170.         break;
  171.         default:
  172.         case 5: 
  173.         (void) fprintf(b,"\\paragraph{");
  174.         break;
  175.      }
  176.     if (islower(string[0]))
  177.      string[0] = toupper(string[0]);
  178.     puttex(string,b);
  179.     (void) fprintf(b,"}\n");
  180. }
  181.  
  182. /* put text in string str to file while buffering special TeX characters */
  183. puttex(str,file)
  184. FILE *file;
  185. register char *str;
  186. {
  187. register char ch;
  188. static boolean inquote = FALSE;
  189.  
  190.      while( (ch = *str++) != '\0') {
  191.          switch(ch) {
  192.              case '#':
  193.              case '$':
  194.              case '%':
  195.              case '&':
  196.              case '_':
  197.              case '{':
  198.              case '}':
  199.                  (void) fputc('\\',file);
  200.                  (void) fputc(ch,file);
  201.                  break;
  202.              case '\\':
  203.                  (void) fputs("$\\backslash$",file);
  204.                  break;
  205.              case '~':
  206.                  (void) fputs("\\~{\\ }",file);
  207.                  break;
  208.              case '^':
  209.                  (void) fputs("\\verb+^+",file);
  210.                  break;
  211.              case '>':
  212.              case '<':
  213.              case '|':
  214.                  (void) fputc('$',file);
  215.                  (void) fputc(ch,file);
  216.                  (void) fputc('$',file);
  217.                  break;
  218.              case '"': 
  219.                  /* peek at next character: if space, end of quote */
  220.                  if (*str == NULL || isspace(*str) || ispunct(*str))
  221.                    (void) fputs("''", file);
  222.                  else
  223.                    (void) fputs("``", file);
  224.                  break;
  225.              case '`':    /* backquotes mean boldface */
  226.                  if (inquote) {
  227.                     fputs("}", file);
  228.                     inquote = FALSE;
  229.                  } else {
  230.                     fputs("{\\bf ", file);
  231.                     inquote = TRUE;
  232.                  }
  233.                  break;
  234.              default:
  235.                  (void) fputc(ch,file);
  236.                  break;
  237.          }
  238.      }
  239. }
  240.  
  241.  
  242. finish(b)
  243. FILE *b;
  244. {
  245.     (void) fputs("\\end{document}\n",b);
  246. }
  247.